package cn.wolfcode.service.impl;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;
    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }
    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }
    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }
    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }
    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        /*int count = permissionMapper.selectForCount(qo);
        if (count == 0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(), Collections.emptyList(),count);
        }
        //获取总页数
        int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }

    @Override
    public void reload() {
        //获取所有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //获取所有的controller
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controller = map.values();
        //遍历每个controller,获取里面的每个方法
        for (Object controllers : controller){
            //如果不是cglib代理对象，不往下执行
            if (!AopUtils.isCglibProxy(controller)){
                continue;
            }

            //获取controller的字节码对象
            Class aClass = controllers.getClass();
            //获取当前字节码中的方法
            Method[] methods = aClass.getDeclaredMethods();
            //遍历每个方法，判断方法上是否有贴上注解
            for (Method method : methods){
                //获取注解对象
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //如果有这个注解
                if (annotation!=null){
                    //直接从注解中获取  0 的位置是权限表达式 1 的位置是名字
//                    String expression = annotation.expression();
                    String expression = annotation.value()[0];
                    //判断权限表达式是否已经存在数据库中
                    if (!permissions.contains(expression)){
                        //获取注解上的权限名称
                        String name = annotation.value()[1];
                        //封装成对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        //插入数据中
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }
    @Override
    public List<String> selectExpressionByEmpId(Long employeeId) {
        return permissionMapper.selectExpressionByEmpId(employeeId);
    }
}




